package com.aigou.admin.controller;

import com.aigou.admin.annotation.ResponseResult;
import com.aigou.admin.entity.*;
import com.aigou.admin.service.BrandService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author chai
 * @date 2024/12/4 0004
 * @Description
 */

@RestController
@RequestMapping("/brand")
@AllArgsConstructor
@ResponseResult
public class BrandController {

    private final BrandService brandService;

    //将方法名称作为参数传递  m=save
    @RequestMapping("/save")
    public Boolean save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("brand_name");
        String type = req.getParameter("brand_type");
        String img=req.getParameter("brand_img");
        return brandService.save(new Brand(id, name, type, img));
    }

    @RequestMapping("/update")
    public Boolean update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
       String id = req.getParameter("id");
        String name = req.getParameter("brand_name");
        String type = req.getParameter("brand_type");
        String img=req.getParameter("brand_img");
        return brandService.update(new Brand(id, name, type, img));
    }

    @RequestMapping("/delete")
    public Boolean delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        return brandService.remove(id);
    }

    @RequestMapping("/findById")
    public Brand findById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        return brandService.findById(id);
    }
    @RequestMapping("/findAll")
    public List<Brand> findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //查询用户
        return brandService.findAll();
    }

    @RequestMapping("/page")
    public PageEntity<Brand> page(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
        return brandService.findBrandsByPage(Integer.parseInt(current), Integer.parseInt(pageSize));
    }
}
